/** 
 * @desc   SVG转PNG
 * @param  {Object} o 转入参数
 * @param  {Element} o.svg svg 节点
 * @param  {Number} o.scale 缩放
 * @param  {String} o.toDataType 输出数据类型 toDataURL||toBlob
 * @param  {Function} o.success 成功回调
 * @param  {Function} o.error 失败回调
 */
function svg2png(o) {
    let svg = o.svg;
    let scale = o.scale || 1;
    let toDataType = o.toDataType || "toDataURL";
    let success = o.success || function () { };
    let error = o.error || function () { };
    let svgData = new XMLSerializer().serializeToString(svg);
    let canvas = document.createElement("canvas");
    let svgSize = svg.getBoundingClientRect();
    canvas.width = svgSize.width * scale;
    canvas.height = svgSize.height * scale;
    // canvas.style.width = svgSize.width;
    // canvas.style.height = svgSize.height;
    let ctx = canvas.getContext("2d");
    ctx.scale(scale, scale);
    let img = document.createElement("img");
    img.setAttribute("src", "data:image/svg+xml;base64," + btoa(unescape(encodeURIComponent(svgData))));
    img.onload = function () {
        ctx.drawImage(img, 0, 0);
        success(canvas[toDataType]("image/png", 1));
    };
    img.onerror = error;
};

//svg2png(document.querySelector("svg"));